package com.gm.camunda.service.impl;

import com.gm.camunda.service.ProcessDefinitionService;
import com.gmadmin.common.ApiResponse;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * {@code @Author}: zhang liang
 * {@code @Date}: 2022/10/9 15:05
 * {@code @BelongsProject}: gm
 * {@code @BelongsPackage}: com.gm.camunda.service.impl
 * {@code @Description}: 自定义流程实现
 */
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {

    public final Logger logger = LoggerFactory.getLogger(this.getClass());
   private final RepositoryService repositoryService;

    public ProcessDefinitionServiceImpl(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @Override
    public ApiResponse deployDefinitionByResource(String name, String resource) {
        Deployment deploy = repositoryService.createDeployment()
                .name(name)
                .addClasspathResource(resource)
                .deploy();
        logger.info("流程Id:" + deploy.getId());
        logger.info("流程Name:" + deploy.getName());
        return ApiResponse.ofSuccess();
    }

    @Override
    public ApiResponse deployDefinitionByString(String definitionName, String resourceName, String resource) {
        Deployment deploy = repositoryService.createDeployment()
                .name(definitionName)
                .addString(resourceName+".bpmn",resource)
                .deploy();
        logger.info("流程Id:" + deploy.getId());
        logger.info("流程Name:" + deploy.getName());
        return ApiResponse.ofSuccess();
    }
}
